﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Docking = WeifenLuo.WinFormsUI.Docking;
using EdytorWF.Engine;
using EdytorWF.Objects;
using Microsoft.DirectX;

namespace EdytorWF
{

    public partial class MainView : Docking::DockContent
    {
        Point lastPoint;
        Camera ViewCamera;
        ViewInfo info;

        public MainView()
        {
            InitializeComponent();

            AllowEndUserDocking = true;
            AutoScaleDimensions = new SizeF(6, 13);
            AutoScaleMode = AutoScaleMode.Font;
            ClientSize = new Size(1000, 1000);
            DockAreas = (Docking.DockAreas)(Docking.DockAreas.Document);
            Name = "Editor View";
            ShowHint = Docking.DockState.Document;
            Text = "View";
            TabText = "View";

            ViewCamera = new Camera();

            IEngine.EngineInstance.SetView(View, ref ViewCamera);
            info = IEngine.EngineInstance.CreateViewInfo();

            View.MouseWheel += new MouseEventHandler(View_MouseWheel);
        }

        //////////////////////////////////////////////////////////////////////////
        public void Repaint()
        {
            UpdateSetInfo();
            IEngine.EngineInstance.UpdateCamera();
        }

        //////////////////////////////////////////////////////////////////////////
        private void UpdateSetInfo()
        {
            if (info != null)
            {
                info.SetViewInfo(ViewCamera.ToString() + "\n" + IEditor.EditorInstance.SelectedObjectName);
            }
            IEngine.EngineInstance.UpdateCamera();
            
        }

        //////////////////////////////////////////////////////////////////////////
        public void SetWireFrame()
        {
            IEngine.EngineInstance.SetWireFrame();
            Repaint();
        }

        //////////////////////////////////////////////////////////////////////////
        public void SetSolid()
        {
            IEngine.EngineInstance.SetSolid();
            Repaint();
        }

        //////////////////////////////////////////////////////////////////////////
        private void View_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            if (e.KeyCode == Keys.W)
            {
                IEngine.EngineInstance.MoveCamera(0, 0, 1);
                IEngine.EngineInstance.FindTerrainCollision();
                IEngine.EngineInstance.MovePersonCamera(IEngine.EngineInstance.SelectedPointOnTerrain);
                Repaint();
            }
            else if (e.KeyCode == Keys.S)
            {
                IEngine.EngineInstance.MoveCamera(0, 0, -1);
                IEngine.EngineInstance.FindTerrainCollision();
                IEngine.EngineInstance.MovePersonCamera(IEngine.EngineInstance.SelectedPointOnTerrain);
                Repaint();
            }
            else if (e.KeyCode == Keys.A)
            {
                IEngine.EngineInstance.MoveCamera(-1, 0, 0);
                IEngine.EngineInstance.FindTerrainCollision();
                IEngine.EngineInstance.MovePersonCamera(IEngine.EngineInstance.SelectedPointOnTerrain);
                Repaint();
            }
            else if (e.KeyCode == Keys.D)
            {
                IEngine.EngineInstance.MoveCamera(1, 0, 0);
                IEngine.EngineInstance.FindTerrainCollision();
                IEngine.EngineInstance.MovePersonCamera(IEngine.EngineInstance.SelectedPointOnTerrain);
                Repaint();
            }
            else if (e.KeyCode == Keys.R)
            {
                IEngine.EngineInstance.MoveCamera(0, -1, 0);
            }
            else if (e.KeyCode == Keys.T)
            {
                IEngine.EngineInstance.MoveCamera(0, 1, 0);
            }
            else if (e.KeyCode == Keys.Up)
            {
                IEngine.EngineInstance.MoveSun(1, 0, 0);
            }
            else if (e.KeyCode == Keys.Down)
            {
                IEngine.EngineInstance.MoveSun(-1, 0, 0);
            }
            else if (e.KeyCode == Keys.Right)
            {
                IEngine.EngineInstance.MoveSun(0, 1, 0);
            }
            else if (e.KeyCode == Keys.Left)
            {
                IEngine.EngineInstance.MoveSun(1, -1, 0);
            }
            else if (e.KeyCode == Keys.Q)
            {
                //IEditor.EngineInstance.CreateModelObject();
                //IEditor.EngineInstance.CreateSoundObject();
                //IEditor.EditorInstance.CreateLightObject();
                IEditor.EditorInstance.PutObject();
            }
        }

        //////////////////////////////////////////////////////////////////////////
        private void View_Paint(object sender, PaintEventArgs e)
        {
            Repaint();
        }

        //////////////////////////////////////////////////////////////////////////
        private bool LeftPressed = false;
        private bool RightPressed = false;
        private bool MiddlePressed = false;

        //////////////////////////////////////////////////////////////////////////
        private void View_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                LeftPressed = true;
                //SelectedObject = IEngine.EngineInstance.FindObjectByCollision(e.Location);
                //IEngine.EngineInstance.FindTerrainCollision();
                IEditor.EditorInstance.OnLeftButtonClick(e.Location);
            }
            if (e.Button == MouseButtons.Right)
            {
                RightPressed = true;
            }
            if (e.Button == MouseButtons.Middle)
            {
                MiddlePressed = true;
            }
            lastPoint = e.Location;
            Repaint();
        }

        //////////////////////////////////////////////////////////////////////////
        private void View_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                LeftPressed = false;
            }
            if (e.Button == MouseButtons.Right)
            {
                RightPressed = false;
            }
            if (e.Button == MouseButtons.Middle)
            {
                MiddlePressed = false;
            }
            Repaint();
        }

        //////////////////////////////////////////////////////////////////////////
        private void View_MouseMove(object sender, MouseEventArgs e)
        {
            float y = e.Location.X - lastPoint.X;
            float x = e.Location.Y - lastPoint.Y;
            lastPoint = e.Location;

            if (RightPressed)
            {
                IEngine.EngineInstance.RotateCamera(x * (-0.1f), y * (-0.1f), 0);           
            }
            if (MiddlePressed)
            {
                IEngine.EngineInstance.MoveCamera(y * (-0.1f), x * (0.1f), 0);
            }
            IEngine.EngineInstance.TrackCollision(e.Location);
            Repaint();
        }

        //////////////////////////////////////////////////////////////////////////
        void View_MouseWheel(object sender, MouseEventArgs e)
        {
            IEngine.EngineInstance.MoveCamera(0, 0, e.Delta / 120);
            Repaint();
        }

        //////////////////////////////////////////////////////////////////////////
        private void View_MouseEnter(object sender, EventArgs e)
        {
            View.Focus();
        }

        //////////////////////////////////////////////////////////////////////////
        private void MainView_Resize(object sender, EventArgs e)
        {
            
        }

        //////////////////////////////////////////////////////////////////////////
        private void View_Resize(object sender, EventArgs e)
        {
            Repaint();
        }
        //////////////////////////////////////////////////////////////////////////
    }
}
